﻿/*
 *	联系人页点击事件
 */

$(document).ready(function () {
    add_contact();
    delete_contact();
    alter_contact_info();
    alter_contact_avatar();
    remover_contact_info();
});


// 添加联系人
function add_contact() {
    $(".contact_add_a").die().live("click", function (e) {
        e.preventDefault();
        var name = $(this).parent().parent().parent().parent().find(".contact_name_input").val();
        var phone = $(this).parent().parent().parent().parent().find(".contact_phone_input").val();
        var d = new Date()
        var vYear = d.getFullYear();
        var vMon = d.getMonth() + 1;
        var vDay = d.getDate();
        var date = vYear + "-" + vMon + "-" + vDay;


        var email = $("#ru_g_id").val();
        email = $.trim(email);



        if (name == "") {
            //              alert("联系人姓名不能为空");
            //          $(this).parent().parent().parent().parent().find(".contact_name_input").val()
            $(this).parent().parent().parent().parent().find("#contact_name_input_id").focus();
            return;
        }
        if (phone == "") {
            $(this).parent().parent().parent().parent().find("#contact_phone_input_id").focus();
            //            $(this).parent().parent().parent().parent().find(".contact_phone_input").focus();
            return;
        }

        $.ajax({
            url: "/Mange_Main/AddContact",
            type: "post",
            cache: false,
            data: {
                contact_refrences_user_email: email,
                contact_name: name,
                contact_phone: phone,
                contact_time: date
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    //                    alert(msg.Message);
                    var contact_new = $("<div class='contact_box_div'>"
                                             + "<!-- 联系人相片 -->"
                                             + "<div class='contact_photo_div'>"
                                             + "    <img src=''>"
                                             + "</div>"
                                             + "<!-- /联系人相片 -->   "
                                             + "<!-- 联系人标签 -->"
                                             + " <div class='contact_tag_div'>"
                                             + "      <li>#标签</li>"
                                             + " </div>"
                                             + " <!-- 联系人信息 -->"
                                             + " <div class='contact_info_div'>"
                                             + "      <ul>"
                                             + "      <li class='contact_info_div_p_li_name'><input type='text'  class='contact_name_input'  maxlength='50' placeholder='联系人' value='" + name + "' /> </li>"
                                             + "     </ul>"
                                             + "      <ul class='contact_info_div_ul'>"
                                             + "      <li class='contact_info_div_p_li'>tel:</li>"
                                             + "      <li class='contact_info_div_p_li_input'><input type='text'  class='contact_phone_input'  maxlength='50' placeholder='电话' value='" + phone + "' /> </li>"
                                             + "      </ul>"
                                             + "  </div>"
                                             + " <!-- 联系人添加时间 -->"
                                             + " <div class='contact_addtime_div'>"
                                             + "  <p class='contact_addtime_div_line'> </p>"
                                             + "       <p> // " + date + " <input class='hide_contactid_input' type='hidden'; value='" + msg.Message + "'/>"
                                             + "       <span class='contact_manage_span' ><a class='contact_alter_a'></a><a class='contact_delete_a'></a><a class='contact_avatar_a'></a><a class='contact_recover_a'></a><span>"
                                             + "       </p>"
                                             + " </div>"
                                             + " </div>");
                    //                    $("#right_contact_div").append(contact_new);
                    $('#right_contact_div').prepend(contact_new).masonry('reload');
                    //                    var thishandler = $(this);
                    //                    $('#right_contact_div').masonry('remove', this);
                    $("#contact_box_div_id").remove();
                    var new_contact_div = $("<div id='contact_box_div_id' class='contact_box_div'>"
                         + "<!-- 联系人相片 -->"
                         + "<div class='contact_photo_div'>"
                         + "    <img src='../../Content/image/system/contact/undefine.png'>"
                         + "</div>"
                         + "<!-- /联系人相片 -->   "
                         + "<!-- 联系人标签 -->"
                         + " <div class='contact_tag_div'>"
                         + "      <li>#标签</li>"
                         + " </div>"
                         + " <!-- 联系人信息 -->"
                         + " <div class='contact_info_div'>"
                         + "      <ul>"
                         + "      <li class='contact_info_div_p_li_name'><input type='text'  name='username' id='contact_name_input_id' class='contact_name_input'  maxlength='50' placeholder='联系人姓名' /> </li>"
                         + "     </ul>"
                         + "      <ul class='contact_info_div_ul'>"
                         + "      <li class='contact_info_div_p_li'>tel:</li>"
                         + "      <li class='contact_info_div_p_li_input'><input type='text'  name='username' id='contact_phone_input_id' class='contact_phone_input'  maxlength='50' placeholder='联系人电话' /> </li>"
                         + "      </ul>"
                         + "  </div>"
                         + " <!-- 联系人添加时间 -->"
                         + " <div class='contact_addtime_div'>"
                         + "  <p class='contact_addtime_div_line'> </p>"
                         + "       <p> // " + date + "<a class='contact_add_a'></a></p>"
                         + " </div>"
                         + " </div>");
                    $('#right_contact_div').prepend(new_contact_div).masonry('reload');
                    $('#right_contact_div').find("#contact_name_input_id").focus();
                    //                    $("#right_contact_div").append(new_contact_div);
                    //                    $('#right_contact_div').masonry('reloadItems');
                    //                    $('#right_contact_div').append(contact_new).masonry('appended', contact_new);
                    umc_waterfallset(); //瀑布流一下
                   
                } else {
                    alert("修改失败");
                }
            }
        });

    });
}


// 删除联系人
function delete_contact() {
    $(".contact_delete_a").die().live("click", function (e) {
        e.preventDefault();
        var contactid = $(this).parent().parent().parent().parent().find(".hide_contactid_input").val();
        var thishandle = $(this).parent().parent().parent().parent();

        var truthBeTold = window.confirm("确定删除?");
        if (truthBeTold) {
            $.ajax({
                url: "/Mange_Main/DeleteContact",
                type: "post",
                cache: false,
                data: { contactid: contactid },
                dataType: "json",
                success: function (msg) {
                    if (msg.Result) {
                        thishandle.remove();
                        umc_waterfallset(); //瀑布流一下
                    }
                    else {
                    alert(msg.Message);
                    }

                }
            }); 
        } 

    });
}

// 修改联系人信息
function alter_contact_info() {
    $(".contact_alter_a").die().live("click", function (e) {
        e.preventDefault();

        var contactid = $(this).parent().parent().parent().parent().find(".hide_contactid_input").val();
        var contact_name = $(this).parent().parent().parent().parent().find(".contact_name_input").val();
        var contact_phone = $(this).parent().parent().parent().parent().find(".contact_phone_input").val();


        if (contact_name == "") {
            $(this).parent().parent().parent().parent().find(".contact_name_input").focus();
            return;
        }
        if (contact_phone == "") {
            $(this).parent().parent().parent().parent().find(".contact_phone_input").focus();
            return;
        }


        $.ajax({
            url: "/Mange_Main/AlterContact",
            type: "post",
            cache: false,
            data: { contactid: contactid, contact_name: contact_name, contact_phone: contact_phone },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    alert(msg.Message);
                }
                else {
                    alert(msg.Message);
                }

            }
        }); 



        var thishandle = $(this).parent().parent().parent().parent();
    });
}
// 修改联系人头像
function alter_contact_avatar() {
    $(".contact_avatar_a").die().live("click", function (e) {
        e.preventDefault();
    });
}

// 恢复联系人数据
function remover_contact_info() {
    $(".contact_recover_a").die().live("click", function (e) {
        e.preventDefault();

        var contactid = $(this).parent().parent().parent().parent().find(".hide_contactid_input").val();

        var thishandle = $(this).parent().parent().parent().parent();
        $.ajax({
            url: "/Mange_Main/GetRemoverContact",
            type: "post",
            cache: false,
            data: { contactid: contactid },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    thishandle.find(".contact_name_input").val(msg.contact_name);
                    thishandle.find(".contact_phone_input").val(msg.contact_phone);
                }
                else {
                    alert(msg.Message);
                }

            }
        });
    });
}